<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>介绍 | 微观世界</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/logo.jpg">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="计算机软件 + 物联网 + 云计算项目开发、设计、咨询">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.55f00579.css" as="style"><link rel="preload" href="/assets/js/app.92627f89.js" as="script"><link rel="preload" href="/assets/js/2.817fcd47.js" as="script"><link rel="preload" href="/assets/js/19.98ca7c32.js" as="script"><link rel="prefetch" href="/assets/js/10.791ab255.js"><link rel="prefetch" href="/assets/js/11.1dc01403.js"><link rel="prefetch" href="/assets/js/12.c421b677.js"><link rel="prefetch" href="/assets/js/13.8fa15714.js"><link rel="prefetch" href="/assets/js/14.2cca4cf4.js"><link rel="prefetch" href="/assets/js/15.e9f5696c.js"><link rel="prefetch" href="/assets/js/16.62cab094.js"><link rel="prefetch" href="/assets/js/17.6c789b79.js"><link rel="prefetch" href="/assets/js/18.74f9e612.js"><link rel="prefetch" href="/assets/js/20.5052a859.js"><link rel="prefetch" href="/assets/js/21.3ea5972a.js"><link rel="prefetch" href="/assets/js/22.18d546b5.js"><link rel="prefetch" href="/assets/js/23.069cf840.js"><link rel="prefetch" href="/assets/js/3.62c9d964.js"><link rel="prefetch" href="/assets/js/4.58e718d2.js"><link rel="prefetch" href="/assets/js/5.addf51f6.js"><link rel="prefetch" href="/assets/js/6.a0e5b372.js"><link rel="prefetch" href="/assets/js/7.5d4ccb0f.js"><link rel="prefetch" href="/assets/js/8.ad529520.js"><link rel="prefetch" href="/assets/js/9.cc29df21.js">
    <link rel="stylesheet" href="/assets/css/0.styles.55f00579.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">微观世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/" class="active sidebar-link">介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/1.x/#它是如何工作的" class="sidebar-link">它是如何工作的</a></li><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/1.x/#路由" class="sidebar-link">路由</a></li><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/1.x/#插件" class="sidebar-link">插件</a></li><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/1.x/#faq" class="sidebar-link">FAQ</a></li></ul></li><li><a href="/ws-cloud/gateway/1.x/getting-started.html" class="sidebar-link">快速上手</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/" class="sidebar-link">介绍</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/dev.html" class="sidebar-link">插件开发</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>官方插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/official/discovery.html" class="sidebar-link">discovery plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/rewrite.html" class="sidebar-link">rewrite plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/tracing.html" class="sidebar-link">tracing plugin</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="介绍"><a href="#介绍" class="header-anchor">#</a> 介绍</h1> <p>相对 0.x 版本，对代码进行了重构。插件化设计：将各功能点拆分成了插件，同时支持自定义插件，满足不同业务团队（当然也适合大规模团队定制化开发）。同时还增加更多CI工具完善了测试用例</p> <h2 id="它是如何工作的"><a href="#它是如何工作的" class="header-anchor">#</a> 它是如何工作的</h2> <ol><li>每个请求会根据请求 url 获取到请求对应的路由配置，如无法匹配则创建默认路由。路由配置包括插件配置、参数配置，请求协议等<div class="language-lua extra-class"><pre class="language-lua"><code> <span class="token comment">-- gateway/app/core/ctx.lua</span>

 <span class="token comment">-- 从上下文中获取dispatcher</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> ngx_ctx <span class="token operator">=</span> ngx<span class="token punctuation">.</span>ctx
     <span class="token keyword">local</span> ctx_dispatcher <span class="token operator">=</span> ngx_ctx<span class="token punctuation">.</span>dispatcher
     <span class="token keyword">if</span> <span class="token keyword">not</span> ctx_dispatcher <span class="token keyword">then</span>
         <span class="token keyword">local</span> route <span class="token operator">=</span> route_store<span class="token punctuation">.</span><span class="token function">get_route_by_uri</span><span class="token punctuation">(</span>ngx<span class="token punctuation">.</span>var<span class="token punctuation">.</span>uri<span class="token punctuation">)</span>
         <span class="token keyword">if</span> <span class="token keyword">not</span> route <span class="token keyword">then</span>
             route <span class="token operator">=</span> <span class="token punctuation">{</span>
                 prefix <span class="token operator">=</span> ngx<span class="token punctuation">.</span>var<span class="token punctuation">.</span>uri<span class="token punctuation">,</span>
                 status <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
                 service_name <span class="token operator">=</span> <span class="token string">&quot;default&quot;</span><span class="token punctuation">,</span>
                 protocol <span class="token operator">=</span> <span class="token string">&quot;http&quot;</span><span class="token punctuation">,</span>
                 plugins <span class="token operator">=</span> <span class="token punctuation">{</span>
                     <span class="token string">&quot;default&quot;</span>
                 <span class="token punctuation">}</span>
             <span class="token punctuation">}</span>
         <span class="token keyword">end</span>

         <span class="token keyword">local</span> dispatcher_plugins <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
         <span class="token keyword">for</span> _<span class="token punctuation">,</span> plugin_name <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span>route<span class="token punctuation">.</span>plugins<span class="token punctuation">)</span> <span class="token keyword">do</span>
             dispatcher_plugins<span class="token punctuation">[</span>plugin_name<span class="token punctuation">]</span> <span class="token operator">=</span> plugins<span class="token punctuation">[</span>plugin_name<span class="token punctuation">]</span>
         <span class="token keyword">end</span>
         ctx_dispatcher <span class="token operator">=</span> dispatcher<span class="token punctuation">:</span><span class="token function">new</span><span class="token punctuation">(</span>dispatcher_plugins<span class="token punctuation">,</span> route<span class="token punctuation">)</span>
         ngx_ctx<span class="token punctuation">.</span>dispatcher <span class="token operator">=</span> ctx_dispatcher
     <span class="token keyword">end</span>
     <span class="token keyword">return</span> ctx_dispatcher
 <span class="token keyword">end</span>
</code></pre></div></li> <li>根据路由配置创建 dispatcher 实例，同时会放到 ngx.ctx 上下文中</li> <li>dispatcher 实例在每个 ngx 执行阶段中都会执行回应钩子函数<div class="language-lua extra-class"><pre class="language-lua"><code> <span class="token comment">-- gateway/app/init.lua</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token comment">-- 加载配置文件</span>
     <span class="token keyword">local</span> config_file <span class="token operator">=</span> os<span class="token punctuation">.</span><span class="token function">getenv</span><span class="token punctuation">(</span><span class="token string">&quot;config.file&quot;</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token string">&quot;conf/app.json&quot;</span>
     config<span class="token punctuation">:</span><span class="token function">init</span><span class="token punctuation">(</span>config_file<span class="token punctuation">)</span>

     <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_init_worker</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> ctx <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> worker_id <span class="token operator">=</span> ngx<span class="token punctuation">.</span>worker<span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;init worker: &quot;</span><span class="token punctuation">,</span> worker_id<span class="token punctuation">)</span>

     ctx<span class="token punctuation">.</span><span class="token function">init_worker</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_rewrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_rewrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_access</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_access</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_content</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_content</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_balancer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_balancer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_header_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_header_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_body_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_body_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>

 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">http_log</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token keyword">local</span> dispatcher <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;app.core.ctx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get_dispatcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     dispatcher<span class="token punctuation">:</span><span class="token function">do_in_log</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
 <span class="token keyword">end</span>
</code></pre></div></li> <li>dispatcher 各阶段的钩子函数会执行对应插件的钩子函数</li> <li>整个请求的处理逻辑，完全由每个插件各阶段的执行逻辑决定</li></ol> <p>参考：<a href="https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/phase.html" target="_blank" rel="noopener noreferrer">https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/phase.html<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="路由"><a href="#路由" class="header-anchor">#</a> 路由</h2> <p>路由是整个网关流量分发的基础，网关会根据路由匹配获取路由配置，通过配置将插件加载到请求上下文中，并在每个阶段执行对应插件处理逻辑。</p> <div class="language- extra-class"><pre class="language-text"><code>+--------+     +---------+     +------------+      
| 请求URL |+---&gt;| 匹配路由 |+---&gt;| 执行插件逻辑 |
+--------+     +---------+     +------------+      
</code></pre></div><p>路由匹配完全由<code>lua-resty-radixtree</code>实现，具体匹配规则参考：<a href="https://github.com/api7/lua-resty-radixtree" target="_blank" rel="noopener noreferrer">https://github.com/api7/lua-resty-radixtree<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="插件"><a href="#插件" class="header-anchor">#</a> 插件</h2> <ul><li>discovery 插件：根据请求url路由到上游服务节点，支持 http 和 grpc 协议</li> <li>rewrite 插件：重写请求url，将网关 url 转换成实际上游服务 url</li> <li>tracing 插件：为每个请求生成 trace-id，通过 header 传递，同时会输出到accesslog，方便做链路跟踪</li></ul> <h2 id="faq"><a href="#faq" class="header-anchor">#</a> FAQ</h2> <ul><li>gRPC 代理配置查看 <a href="/ws-cloud/gateway/1.x/plugin/official/discovery.html">plugin/official/discovery.html</a></li></ul></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/ws-cloud/gateway/1.x/getting-started.html">
        快速上手
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.92627f89.js" defer></script><script src="/assets/js/2.817fcd47.js" defer></script><script src="/assets/js/19.98ca7c32.js" defer></script>
  </body>
</html>
